package top.sxlai.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.sxlai.constant.LoginStatus;
import top.sxlai.constant.OtherStatus;
import top.sxlai.entity.DTO.LoginRequest;
import top.sxlai.entity.DTO.OpUserInfoDto;
import top.sxlai.entity.OpUser;
import top.sxlai.mapper.OpUserMapper;
import top.sxlai.service.OpUserService;
import top.sxlai.util.JwtUtil;
import top.sxlai.util.oss.UploadAvatarFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
 * (OpUser)表服务实现类
 *
 * @author makejava
 * @since 2024-04-06 16:01:23
 */
@Slf4j
@Service("opUserService")
public class OpUserServiceImpl extends ServiceImpl<OpUserMapper, OpUser> implements OpUserService {
    @Autowired
    private OpUserMapper opUserMapper;

    @Autowired
    private UploadAvatarFile uploadAvatarFile;

    /**
     * 执行登录的逻辑
     *
     * @param loginRequest 参数
     * @return ResponseEntity<Map < String, String>>
     */
    @Override
    public ResponseEntity<Map<String, String>> login(LoginRequest loginRequest) {
        OpUser opUser = opUserMapper.login(loginRequest.getUsername(), loginRequest.getPassword());
        Map<String, String> response = new HashMap<>();
        if (opUser != null) {
            log.info("用户：{} 登录成功, 时间:{}", loginRequest.getUsername(), LocalDateTime.now());
            //登录成功,设置token
            String token = JwtUtil.createJWT(UUID.randomUUID().toString(), String.valueOf(opUser.getUserid()), null);
            response.put("Authorization", token);
            response.put("userId", String.valueOf(opUser.getUserid()));
            response.put("username", opUser.getUsername());
            response.put("nickName", opUser.getNickname());
            response.put("isVip", String.valueOf(opUser.getIsvip()));
            response.put("headImageUrl", opUser.getHeadimageurl());
            return ResponseEntity.ok(response);
        } else {
            //登录失败
            log.info("用户：{} 登录失败, 时间:{}", loginRequest.getUsername(), LocalDateTime.now());
            response.put("message", LoginStatus.FAILURE.getMessage());
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
        }
    }

    /**
     * 上传头像文件
     *
     * @param avatarFile 头像文件
     * @param userId     用户id
     * @return ResponseEntity<Map < String, String>>
     */
    @Override
    public ResponseEntity<Map<String, String>> uploadAvatarFile(MultipartFile avatarFile, String userId) {

        log.info("开始上传头像文件:{},时间:{}", avatarFile.getName(), LocalDateTime.now());
        //上传文件到oss对象存储并返回一个地址
        try {
            String avatarUrl = uploadAvatarFile.uploadFile(avatarFile);
            //更新数据库中的头像地址
//            int flag = opUserMapper.updateAvatarUrl(avatarUrl, userId);
            Map<String, String> response = new HashMap<>();
            response.put("avatarUrl", avatarUrl);
            if (avatarUrl != null) {
                log.info("上传头像文件成功:{},时间:{}", avatarFile.getName(), LocalDateTime.now());
                return ResponseEntity.ok(response);
            } else {
                log.info("上传头像文件失败:{},时间:{}", avatarFile.getName(), LocalDateTime.now());
                response.put("message", OtherStatus.FAILURE.getMessage());
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改用户信息
     *
     * @param opUserInfoDto 修改的数据
     * @return ResponseEntity<Map < String, String>>
     */
    @Override
    public ResponseEntity<Map<String, String>> changeUserInfo(OpUserInfoDto opUserInfoDto) {
        log.info("开始修改用户信息,时间:{}", LocalDateTime.now());
        Map<String, String> response = new HashMap<>();

        int flag = opUserMapper.changeUserInfo(opUserInfoDto);

        try {
            if (flag != 0) {
                log.info("修改用户信息成功,时间:{}", LocalDateTime.now());
                response.put("message", OtherStatus.SUCCESS.getMessage());
                return ResponseEntity.ok(response);
            } else {
                log.info("修改用户信息失败,时间:{}", LocalDateTime.now());
                response.put("message", OtherStatus.FAILURE.getMessage());
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改密码
     * @param opUserInfoDto 用户数据
     * @return ResponseEntity<Map < String, String>>
     */

    @Override
    public ResponseEntity<Map<String, String>> changePassword(OpUserInfoDto opUserInfoDto) {
        log.info("开始修改密码,时间:{}", LocalDateTime.now());
        Map<String, String> response = new HashMap<>();

        int flag = opUserMapper.changePassword(opUserInfoDto);

        try {
            if (flag != 0) {
                log.info("修改密码成功,时间:{}", LocalDateTime.now());
                response.put("message", OtherStatus.CHANGESUCESS.getMessage());
                return ResponseEntity.ok(response);
            } else {
                log.info("修改密码失败,时间:{}", LocalDateTime.now());
                response.put("message", OtherStatus.CHANGEFAILURE.getMessage());
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}